package com.netty.cn.rpc.protoc.client;

import com.netty.cn.rpc.protoc.RpcProtocClient;
import com.netty.cn.rpc.protoc.model.ServerModel;
import com.netty.cn.rpc.protoc.model.ServerModel.ServerDataModel;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class ProtocClientHandler extends SimpleChannelInboundHandler<ServerModel.ServerDataModel> {
	
	
	
	private RpcProtocClient rpcProtocClient;
    
    public ProtocClientHandler(RpcProtocClient rpcProtocClient) {
        this.rpcProtocClient = rpcProtocClient;
    }

	/**
      * 连接
      */
	@Override
    public void channelActive(ChannelHandlerContext ctx) {
    	System.out.println("连接成功："+ctx.channel().remoteAddress());
    }
	
	/**
	 * 接收服务端消
	 */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ServerDataModel msg) {
    	System.out.println("服务端返回消息为："+msg.getMsg());
    }
    
    
    /**
     * 关闭重连
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    	super.channelInactive(ctx);
    	rpcProtocClient.doConnect();
    }

}
